<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/11/18
 * Time: 18:04
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Keyword extends Base
{
    /**
     * 关键字列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('keyword')->count();// 获取总条数
            $lists = Db::name('keyword')
                ->field('keyword_id, keyword_name, keyword_datetime, keyword_updatetime')
                ->page($Nowpage,$limits)
                ->order('keyword_datetime desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加关键字
     * @return mixed|\think\response\Json
     */
    public function keywordAdd(){
        if(Request::instance()->isPost()){
            $keyword_name = trim(input('keyword_name'), ' ');
            $exist = Db::table('up_keyword')->where('keyword_name', $keyword_name)->find();
            if($exist) return json(['code' => 100, 'msg' => '关键字重复']);
            
            $res = Db::table('up_keyword')->insert([
                'keyword_name' => $keyword_name,
                'keyword_datetime' => date('Y-m-d H:i:s'),
                'keyword_updatetime' => date('Y-m-d H:i:s')
            ]);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('keyword/keywordadd');
    }
    
    /**
     * 编辑关键字
     * @return mixed|\think\response\Json
     */
    public function keywordEdit(){
        $keyword_id = input('keyword_id');
        if(Request::instance()->isPost()){
            $keyword_name = trim(input('keyword_name'), ' ');
            $exist = Db::table('up_keyword')
                ->where('keyword_name', $keyword_name)
                ->where('keyword_id', 'NEQ', $keyword_id)
                ->find();
            if($exist) return json(['code' => 100, 'msg' => '关键字重复']);
            
            $res = Db::table('up_keyword')
                ->where('keyword_id', $keyword_id)
                ->update([
                    'keyword_name' => $keyword_name,
                    'keyword_updatetime' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $keyword = Db::table('up_keyword')->where('keyword_id', $keyword_id)->value('keyword_name');
        $this->assign([
            'keyword' => $keyword,
            'keyword_id' => $keyword_id
        ]);
        return $this->fetch('keyword/keywordedit');
    }
    
    /**
     * 删除关键字
     * @return \think\response\Json
     */
    public function keywordDel(){
        $keyword_id = input('id');
        $res = Db::table('up_keyword')->delete($keyword_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}